/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.client.gui.panels;

import edu.uoc.tdp.pac3.bo.IIncidenciaBO;
import edu.uoc.tdp.pac3.client.gui.model.AreaModelComboBox;
import edu.uoc.tdp.pac3.client.gui.model.HistoricIncidenciaTableModel;
import edu.uoc.tdp.pac3.client.gui.model.RequestModelComboBox;
import edu.uoc.tdp.pac3.client.gui.model.RequestNoteModel;
import edu.uoc.tdp.pac3.client.gui.model.UserModelComboBox;
import edu.uoc.tdp.pac3.domain.Request;
import edu.uoc.tdp.pac3.domain.RequestType;
import java.rmi.RemoteException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Gerard
 */
public abstract class BaseIncidenciaPanel extends JPanel {
    
    private IIncidenciaBO incidenciaBO;
    
    private ResourceBundle resourceBundle;

    public BaseIncidenciaPanel(IIncidenciaBO incidenciaBO) {
        this.incidenciaBO = incidenciaBO;
        this.resourceBundle = ResourceBundle.getBundle("edu.uoc.tdp.pac3.client.gui.panels.messages");
    }
    
    public BaseIncidenciaPanel(IIncidenciaBO incidenciaBO, ResourceBundle resourceBundle) {
        this.incidenciaBO = incidenciaBO;
        this.resourceBundle = resourceBundle;
    }

    public IIncidenciaBO getIncidenciaBO() {
        return incidenciaBO;
    }

    public void setIncidenciaBO(IIncidenciaBO incidenciaBO) {
        this.incidenciaBO = incidenciaBO;
    }

    public String getText(String key){
        return resourceBundle.getString(key);
    }
    
    protected abstract void setLabels();
    
    
    /*
     * GETTING MODELS:
     */
    
    protected AreaModelComboBox getAreaModelComboBox() {
        AreaModelComboBox model = null;
        try {
            model = new AreaModelComboBox(getIncidenciaBO());
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("area.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
    protected UserModelComboBox getUserModelComboBox(){
        UserModelComboBox model = null;
        try {
            model = new UserModelComboBox(incidenciaBO);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("user.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
    protected HistoricIncidenciaTableModel getHistoricIncidenciaTableModel(RequestType requestType){
        HistoricIncidenciaTableModel model = null;
        try {
            model = new HistoricIncidenciaTableModel(requestType);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("historic.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
    protected RequestNoteModel getRequestNoteModel(Request request){
        RequestNoteModel model = null;
        try {
            model = new RequestNoteModel(incidenciaBO, request);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("requestNote.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
    protected RequestModelComboBox getRequestModelComboBox(List<Request> request){
        RequestModelComboBox model = null;
        try {
            model = new RequestModelComboBox(request);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("request.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
    
        protected RequestModelComboBox getRequestModelComboBox(){
        RequestModelComboBox model = null;
        try {
            model = new RequestModelComboBox(incidenciaBO);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog 
                    (null, getText("request.model.error"), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BaseIncidenciaPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
    }
        
    protected void showMessage(boolean okOrFail, String patternKey){
        if(okOrFail){
            showOkMessage(patternKey + ".ok");
        }else {
            showFailMessage(patternKey + ".fail");
        }
    }
    
    private void showOkMessage(String key){
             JOptionPane.showMessageDialog(null, getText(key));
    }
    
    private void showFailMessage(String key){
        JOptionPane.showMessageDialog 
                    (null, getText(key), "Error", JOptionPane.ERROR_MESSAGE);
    }
    
    
    
}
